package com.xayy.health.mgt.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.kawins.base.result.CheckResult;
import cn.kawins.mybatis.base.BaseServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.xayy.health.core.utils.FileInfosUtils;
import com.xayy.health.mgt.mapper.FaultModManageMapper;
import com.xayy.health.mgt.model.faultmodmanage.pojo.dto.FaultModManageDTO;
import com.xayy.health.mgt.model.FaultModManage;
import com.xayy.health.mgt.model.faultmodmanage.pojo.vo.FaultModManageVO;
import com.xayy.health.mgt.service.FaultModManageService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
* @Description: FaultModManageService实现类
* @author liuyuan
* @date 2025-06-05
*/
@Service
@AllArgsConstructor
public class FaultModManageServiceImpl extends BaseServiceImpl<FaultModManage> implements FaultModManageService {
        @Autowired
        private FaultModManageMapper faultModManageMapper;
        /**
        * 获取分页对象
        * @param faultModManageDTO   分页参数
        */
        @Override
        public PageInfo<FaultModManage> pageList(FaultModManageDTO faultModManageDTO) {
                PageMethod.startPage(faultModManageDTO.getPage(), faultModManageDTO.getSize());
                List<FaultModManage> faultModManages = null;
                faultModManages = faultModManageMapper.pageList(faultModManageDTO);
                return new PageInfo<>(faultModManages);
        }



        @Override
        public CheckResult<?> saveFaultMod(FaultModManageVO faultModManageVO, MultipartFile algorithmFile) {
                // 上传文件
                if (BeanUtil.isEmpty(algorithmFile)) {
                        return CheckResult.fail("算法文件不能为空");
                }
                String filePath = "";
                try {
                        filePath = FileInfosUtils.uploadZip(algorithmFile);
                } catch (Exception e) {
                        return CheckResult.fail(e.getMessage());
                }
                // 校验name是否重复
                List<FaultModManage> selectName =
                        faultModManageMapper.selectByName(faultModManageVO.getModName());
                if (CollUtil.isNotEmpty(selectName)) {
                        return CheckResult.fail("算法code:" + faultModManageVO.getModName() + "已存在");
                }
                FaultModManage faultModManage = FaultModManage.builder()
                        .modName(faultModManageVO.getModName())
                        .dataType(faultModManageVO.getDataType())
                        .status(-1)
                        .version(faultModManageVO.getVersion())
                        .httpAddr(faultModManageVO.getHttpAddr())
                        .dirPath(filePath)
                        .startCommad(faultModManageVO.getStartCommad())
                        .createTime(new Date())
                        .build();
                faultModManageMapper.insert(faultModManage);
                return CheckResult.success("算法新建成功");
        }

}
